package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
  * @author Lucie
 *class Delete_Item_Controler
 *Class variables:msg,flag
 *Class methods:delete_Item,check_input,get_msg,set_msg
 */
public class Delete_Item_Controler {
	private String msg; 
	private boolean flag;
	/**
	 *  Constructor initializes the class variables
	 */
	public Delete_Item_Controler()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 * 
	 * @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}

	/**
	 * 
	 * @param book_id
	 	*  this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 *than we continue check by calling the method check_deleteItem that return "copynotonshelf" if one of the items is not on shelf
	 *or "false" if the item not exist or "ok" if we can continue and delete the item and all of his copies with the method deleteItem,
	 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
	 public boolean delete_Item(String book_id)
		{
		 ArrayList<String> str_check=new  ArrayList<String>();
		 	ArrayList<String> str_copy=new  ArrayList<String>();
	 
		 check_input(book_id);
		 	if (flag==false)
		 return false;
		 	str_check=BusinessImplements.GetInstance().getServerProxy().check_deleteItem(book_id);
		 	if(str_check.get(0).equals("copynotonshelf"))
		 	{
		 		set_msg("\ncan't delete this item because one of hes copys is not on shelf"); 
		 		return false;
		 	}
		 	if(str_check.get(0).equals("false"))
		 	{
		 		set_msg("\nitem not exist"); 
		 		return false;
		 	}
		 	if(str_check.get(0).equals("ok"))
		 	{
		 	str_copy=BusinessImplements.GetInstance().getServerProxy().deleteItem(book_id);
			
		 	if (str_copy.get(0).equals("false"))
		 	{
		 		set_msg("\nDelete failed\nPlease try again"); 
		 		return false;
		 	}
		 	}
		 	
		 	return flag;
 	
}
	 /**
	  * This method checks the parameters we have received are not empty
	 * if they are poses flag= false;
	  * @param book_id
	  */
		public void check_input(String book_id)
		{
			
			if(book_id.equals(""))
			{
				set_msg("\nmising book id ");
				flag=false;
			}
		
			
		}
	
	
	

}
